package org.medusa.db.handlersocket;

import org.medusa.db.SyncDBConnector;
import org.medusa.log.ILog;

public class HandlerSocket extends SyncDBConnector {

	private String name; // 以后增加的连接名使用 name:host:port:1 //:1是为了处理一个数据库主机有多个连接的情况
	// private ILog log;

	private HSConnector conn_r;
	private HSConnector conn_wr;

	public HandlerSocket(String name, ILog log) {
		this.name = name;
		// this.log = log;
		this.conn_r = new HSConnector(this.name + "(r)", log, false);
		this.conn_wr = new HSConnector(this.name + "(wr)", log, false);
	}
	
	public Error Connect(String host_r, int port_r, String passwd_r,
			String host_wr, int port_wr, String passwd_wr) {
		this.conn_r.Connect(host_r, port_r, passwd_r);
		return this.conn_wr.Connect(host_wr, port_wr, passwd_wr);
	}

	@Override
	public boolean IsConnected() {
		return this.conn_r.IsConnected() && this.conn_wr.IsConnected();
	}

	@Override
	public Error Close() {
		this.conn_r.Close();
		return this.conn_wr.Close();
	}

	@Override
	public InsertResult Insert(String dbName, String tableName,
			String indexName, String[] columns, Object[] vals) {
		return this.conn_wr.Insert(dbName, tableName, indexName, columns, vals);
	}

	@Override
	public ModifyResult Modify(String dbName, String tableName,
			String indexName, String oper, Object[] keys, String[] columns,
			Object[] newvals, int limit, int skip) {
		return this.conn_wr.Modify(dbName, tableName, indexName, oper, keys,
				columns, newvals, limit, skip);
	}

	@Override
	public ModifyResult Remove(String dbName, String tableName,
			String indexName, String oper, Object[] keys, int limit, int skip) {
		return this.conn_wr.Remove(dbName, tableName, indexName, oper, keys,
				limit, skip);
	}

	@Override
	public QueryResult Query(String dbName, String tableName, String indexName,
			String oper, Object[] keys, String[] columns, int limit, int skip) {
		return this.conn_r.Query(dbName, tableName, indexName, oper, keys,
				columns, limit, skip);
	}

	@Override
	public QueryOneResult QueryOne(String dbName, String tableName,
			String indexName, String oper, Object[] keys, String[] columns,
			int skip) {
		return this.conn_r.QueryOne(dbName, tableName, indexName, oper, keys,
				columns, skip);
	}

}
